package com.em.operate.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;

public class FileUtil {
	
    public static void makeDir(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    
    /**
     * 将steam转换成string对象
     * @param in
     * @return
     * @throws IOException
     */
    public static String stream2Str(InputStream in) throws IOException{
		StringBuffer out = new StringBuffer();
		byte[] b = new byte[4096];
		for (int n; (n = in.read(b)) != -1;) {
			out.append(new String(b, 0, n));
		}
		return out.toString();
    }
    
    /**
     * 将Asset中的内容转换成inputstream
     * @param r
     * @param source
     * @return
     * @throws IOException
     */
    public static InputStream assetRes2Stream(Resources r,String source) throws IOException{
    	return r.getAssets().open(source);
    }
	
    /**
     * 将sample工程需要的资源文件拷贝到SD卡中使用（授权文件为临时授权文件，请注册正式授权）
     * 
     * @param isCover 是否覆盖已存在的目标文件
     * @param source
     * @param dest
     */
    public static void copyFromAssetsToSdcard(boolean isCover,Resources r, String source, String dest) {
        File file = new File(dest);
        if (isCover || (!isCover && !file.exists())) {
        	 InputStream is = null;
            FileOutputStream fos = null;
            try {
            	is = r.getAssets().open(source);
                String path = dest;
                fos = new FileOutputStream(path);
                byte[] buffer = new byte[1024];
                int size = 0;
                while ((size = is.read(buffer, 0, 1024)) >= 0) {
                    fos.write(buffer, 0, size);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
}
